package com.corp.common.base.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.corp.framework.web.filter.RequestCachingWrapper;
import lombok.experimental.UtilityClass;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Keanu
 * @Date 2020/7/16 0016 15:40
 * @Version
 **/
@UtilityClass
public class WebUtils  extends com.corp.platform.common.response.util.WebUtils {

    public static String getRemoteAddr(HttpServletRequest request){
        if(request instanceof RequestCachingWrapper){
            RequestCachingWrapper requestCachingWrapper = (RequestCachingWrapper) request;
            return requestCachingWrapper.getRemoteAddr();
        }
        return getIpAddr(request);
    }

    public static String getRemoteAddr(){
        return getRemoteAddr(getHttpRequest());
    }


    public static String getHeader(String header){
        return getHttpRequest().getHeader(header);
    }


    public static String getMethod(){
        return getHttpRequest().getMethod();
    }


    public static String getRequestURI(){
        return URLUtil.getPath(getHttpRequest().getRequestURI());
    }


    public static String getUserAgent(){
        return getHttpRequest().getHeader("user-agent");
    }

    public static int getServerPort(){
        return getHttpRequest().getServerPort();
    }

    public static String getParams(HttpServletRequest request){
        return HttpUtil.toParams(request.getParameterMap());
    }

    public static String getParams(){
        return getParams(getHttpRequest());
    }

    public static String getBody(HttpServletRequest request){
        if(request instanceof RequestCachingWrapper){
            RequestCachingWrapper requestCachingWrapper = (RequestCachingWrapper) request;
            return requestCachingWrapper.getBody();
        }
        throw new RuntimeException("getBody not support!");
    }

    public static String getBody(){
        return getBody(getHttpRequest());
    }
}
